package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.SysUserService;
import com.iyundao.io.mapper.*;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.MD5Util;
import com.iyundao.io.tools.ResponseData;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by black on 2019/3/20.
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysUserSqlMapper sysUserSqlMapper;

    @Autowired
    private UserCustomMapper userCustomMapper;

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserDeptMapper userDeptMapper;

    @Autowired
    private PartyMemberMapper memberCustomMapper;

    /**
     * 添加管理员
     * @param record
     * @return
     */
    @Override
    public SysUser addSysUser(SysUser record,String sid) {
        String s = UUID.randomUUID().toString();
        record.setSid(s);

        UserDept userDept = new UserDept();
        userDept.setUid(s);
        userDept.setDid(sid);
        userDeptMapper.insert(userDept);

        UserRole userRole = new UserRole();
        userRole.setSid(UUID.randomUUID().toString());
        userRole.setUid(s);
        userRole.setRid("2");
        userRoleMapper.insert(userRole);

        if(sysUserMapper.insertSelective(record) > 0){
            return sysUserMapper.selectByPrimaryKey(record.getSid());
        }else{
            return new SysUser();
        }
    }

    /**
     * 添加领导
     * @param record
     * @param sid
     * @return
     */
    @Override
    public SysUser insertSysUser(SysUser record,String sid) {
        String s = UUID.randomUUID().toString();
        record.setSid(s);

        UserDept userDept = new UserDept();
        userDept.setUid(s);
        userDept.setDid(sid);
        userDeptMapper.insert(userDept);

        UserRole userRole = new UserRole();
        userRole.setSid(UUID.randomUUID().toString());
        userRole.setUid(s);
        userRole.setRid("3");
        userRoleMapper.insert(userRole);

        if(sysUserMapper.insertSelective(record) > 0){
            return sysUserMapper.selectByPrimaryKey(record.getSid());
        }else{
            return new SysUser();
        }
    }

    @Override
    public List<SysUser> getUserByDeptId(String deptId) {
        return sysUserSqlMapper.selectUserByDeptId(deptId);
    }

    @Override
    public List<Map> getUserByNotInDept() {
        return sysUserSqlMapper.selectUserByNotInDept();
    }

    @Override
    public SysUser selectByOpenid(String openid) {
        return userCustomMapper.selectByOpenid(openid);
    }

    @Override
    public SysUserInfo selectInfoByOpenid(String openid) {
        return userCustomMapper.selectInfoByOpenid(openid);
    }

    @Override
    public SysUserInfo addSysUserInfo(SysUserInfo sysUserInfo) {
        sysUserInfo.setSid(UUID.randomUUID().toString());
        if(sysUserInfoMapper.insertSelective(sysUserInfo) > 0){
            return sysUserInfoMapper.selectByPrimaryKey(sysUserInfo.getSid());
        }else{
            return new SysUserInfo();
        }
    }

    @Override
    public SysUser selectByPhone(String phone) {
        return userCustomMapper.selectByPhone(phone);
    }

    @Override
    public int updateSysUserInfo(String uid, String openid) {
        return userCustomMapper.updateSysUserInfo(uid,openid);
    }

    /**
     * 添加管理员
     * @param record
     * @return
     */
    @Override
    public ResponseData addAdministrator(SysUser record, String sid) {
        record.setSid(UUID.randomUUID().toString());
        record.setLoginPwd(MD5Util.encrypt(record.getLoginPwd()));
        if(sysUserMapper.insertSelective(record) > 0){

            //添加角色
            UserRole userRole = new UserRole();
            userRole.setUid(record.getSid());
            userRole.setRid("2");
            userRoleMapper.insert(userRole);

            //添加组织
            UserDept userDept = new UserDept();
            userDept.setUid(record.getSid());
            userDept.setDid(sid);
            int i = userDeptMapper.insert(userDept);

            return new ResponseData(200,"成功");
        }else{
            return new ResponseData(0,"失败");
        }
    }

    @Override
    public ResponseData deleteUser(String sid) {
        userCustomMapper.deleteUser(sid);
        userCustomMapper.deleteUserRole(sid);
        return new ResponseData(200,"成功");
    }

    @Override
    public SysUser selectByLoginName(String name) {
        return userCustomMapper.selectByLoginName(name);
    }

    @Override
    public ResponseData getAdministratorByDeptId(Integer pagenum, Integer pagesize, String deptId) {
        PageHelper.startPage(pagenum,pagesize);
        List<SysUser> list = sysUserSqlMapper.selectAdministratorByDeptId(deptId);
        PageInfo<SysUser> sysUserPageInfo = new PageInfo<>(list);
        return new ResponseData(200,"成功",sysUserPageInfo);
    }

    @Override
    public SysUser selectByPartymemberId(Integer id) {
        PartyMember partyMember = memberCustomMapper.selectById(id);
        return userCustomMapper.selectByPhone(partyMember.getPartymemberphone());
    }
}
